<?php 
require_once("PicInfo.php");
require_once("FileUploader.php");
require_once("MsgStack.php");
require_once("Config.php");

session_start();

$message = new MsgStack();

if ($_FILES["smallPic"]["error"] > 0){
  $message->push("small pic file native Error: " . $_FILES["smallPic"]["error"]);
  $message->isErr = true; 
}

if ($_FILES["largePic"]["error"] > 0){
  $message->push("small pic file native Error: " . $_FILES["smallPic"]["error"]);
  $message->isErr = true; 
}


if(!FileUploader::isFileTypeAvaliable($_FILES["smallPic"]["type"])){
  $message->push("The file type post as thumbnail is not an image");
  $message->isErr = true;  
}

if($_FILES["smallPic"]["size"]>100000){
  $message->push("The size of small Pic must not exceed 100K");
  $message->isErr = true; 
}

if(!FileUploader::isFileTypeAvaliable($_FILES["largePic"]["type"])){
  $message->push("The file type post as thumbnail is not an image");
  $message->isErr = true;  
}

if($_FILES["smallPic"]["size"]>100000){
  $message->push("The size of large Pic must not exceed 100K");
  $message->isErr = true; 
}

if($message->isErr){
  $_SESSION["ERR_ADDPIC"] = $message->getMsg();
  unset($message);
  header("Location:gallery_addPic.php?msg=ERR_ADDPIC");  
  exit();
}


$s_pic_name = $_FILES["smallPic"]["name"];
$l_pic_name = $FILE["largePic"]["name"];

//Save Physical File

$smallPicPath = FileUploader::$STORE_FOLDER.'/s'.FileUploader::generateFileName().'.'.FileUploader::getFileExt($_FILES["smallPic"]["name"]);
$largePicPath = FileUploader::$STORE_FOLDER.'/l'.FileUploader::generateFileName().'.'.FileUploader::getFileExt($_FILES["largePic"]["name"]);
$smallPicRealPath = realpath(FileUploader::$STORE_FOLDER)."/s".FileUploader::generateFileName().'.'.FileUploader::getFileExt($_FILES["smallPic"]["name"]);
$largePicRealPath = realpath(FileUploader::$STORE_FOLDER)."/l".FileUploader::generateFileName().'.'.FileUploader::getFileExt($_FILES["largePic"]["name"]);

$flag_s =  move_uploaded_file($_FILES["smallPic"]["tmp_name"],$smallPicRealPath);
$flag_l =  move_uploaded_file($_FILES["largePic"]["tmp_name"],$largePicRealPath);

if(!$flag_s){
  $message->push("small pic cannot save to remote server.");
  $message->isErr = true; 
}

if(!$flag_l){
  $message->push("large pic cannot save to remote server.");
  $message->isErr = true; 
}

 if($message->isErr){
  $_SESSION["ERR_ADDPIC"] = $message->getMsg();
  unset($message);
  header("Location:gallery_addPic.php?msg=ERR_ADDPIC");  
  exit();
 }
 
 $picInfo=new PicInfo();
//save info to database
$picInfo->setTitle($_POST['title']);
$picInfo->setNote($_POST['note']);
$picInfo->setSmallPic($smallPicPath);
$picInfo->setLargePic($largePicPath);
$picInfo->setAddtime(date("Y-m-d H:i:s"));

$maxOrderQuery = mysql_query("select * from globalVars where id='GALLERY_MAX_ORDER'");
if($maxOrderQuery){
  while($result = mysql_fetch_array($maxOrderQuery)){
    $tmpN = $result['value']+1;
    $picInfo->setOrder($tmpN);
    mysql_query("update globalVars set value=$tmpN. where id='GALLERY_MAX_ORDER'");
  }
}else{
    
  $picInfo->setOrder(1);
    mysql_query("update globalVars set value=1 where id='GALLERY_MAX_ORDER'");
}
$insertSql = "INSERT INTO gallery VALUES (null, ".$picInfo->getOrder().",'".$picInfo->getTitle()."','".$picInfo->getNote()."','".$picInfo->getSmallPic()."','".$picInfo->getLargePic()."','".$picInfo->getAddtime()."')";

$insertPicInfo = mysql_query($insertSql);

if($insertPicInfo){
  $message->push("Insert picture to gallery successful!");
  $_SESSION["MSG_OK_ADDPIC"] = $message->getMsg();
  unset($message);
  unset($picInfo);
  header("Location:gallery_addPic.php?msg=MSG_OK_ADDPIC");  
    
  
}else{
  $message->push("Cannot save picture to gallery");
  $_SESSION["ERR_ADDPIC"] = $message->getMsg();
  unset($message);
  header("Location:gallery_addPic.php?msg=ERR_ADDPIC");  
  
}

?>